﻿#include "deptservice.h"

#include <databasecenter.h>
#include <deptdao.h>
#include <QtDebug>

DeptService::DeptService(QObject *parent) : QObject(parent)
{
    db = DatabaseCenter::getInstance().getConnection();
}

DeptService::~DeptService()
{
    DatabaseCenter::getInstance().releaseConnection(db);
}

bool DeptService::getDeptVOListByPid(QList<DeptVO> &list, uint64_t pid)
{
    bool res = false;
    DeptDao objectDao(db);
    QList<Dept> deptList;
    if (objectDao.getListByPid(deptList,pid))
    {
        res = true;
        for(Dept &item:deptList){
            DeptVO deptVO;
            deptVO.fromDept(item);
            QList<DeptVO> children;
            getDeptVOListByPid(children,item.getId());
            deptVO.setChildren(children);
            list.append(deptVO);
        }
    }
    return res;
}

bool DeptService::getDeptVOList(QList<DeptVO> &list)
{
    return getDeptVOListByPid(list,0);
}

bool DeptService::getList(QList<Dept> &list)
{
    DeptDao objectDao(db);
    return objectDao.getList(list);
}

bool DeptService::getOne(Dept &object)
{
    DeptDao objectDao(db);
    return objectDao.getOne(object);
}

bool DeptService::saveOne(Dept &object)
{
    DeptDao objectDao(db);
    return objectDao.saveOne(object);
}

bool DeptService::deleteOne(Dept &object)
{
    DeptDao objectDao(db);
    return objectDao.deleteOne(object);
}
